/*
 * LogTracer.java
 *
 * Created on 18. Dezember 2006, 20:35
 * 
 *  This file is part of JSmex.
 *
 *  JSmex is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  JSmex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package jsmex;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 *
 * @author Tobias Senger
 */
public class LogTracer {
    
    private FileWriter logfile;
    private BufferedWriter outputStream;
    private String filename;
    private String classname;
    
    /**Opens the logfile writing.
     *
     */
    private void openLogFile() {
        try {
            outputStream = new BufferedWriter(new FileWriter(filename,true));
        } catch (IOException ex) {
            System.out.println("Error while opening Logfile!");
        }
    }
    
    /**Close the Logfile.
     *
     */
    private void closeLogFile() {
        try {
            outputStream.close();
        } catch (IOException ex) {
            System.out.println("Error while closing Logfile!");
        }
    }
    
    /**
     * Creates a new instance of LogTracer
     */
    public LogTracer(String fname) {
        filename = fname;
    }
    
    protected void finalize() {
        closeLogFile();
    }
    
    /**Writes the given info message into the logfile.
     *
     * @param msg The message to write into the logfile.
     * @param classobject The Object which calls the method.
     */
    public void info(String msg, Object classobject) {
        
        classname = classobject.getClass().getName();
        
        openLogFile();
        try {
            outputStream.write(new Date().toString()+"\t"+classname+"\t[INFO] "+msg+"\n");
        } catch (IOException ex) {
            System.out.println("Couldn't write into the logfile!");
        }
        closeLogFile();
    }
    
    /**Writes the given warning message into the logfile.
     *
     * @param msg The message to write into the logfile.
     * @param classobject The Object which calls the method.
     */
    public void warning(String msg, Object classobject) {
        
        openLogFile();
        try {
            outputStream.write(new Date().toString()+"\t"+classname+"\t[WARNING] "+msg+"\n");
        } catch (IOException ex) {
            System.out.println("Couldn't write into the logfile!");
        }
        closeLogFile();
    }
    
    /**Writes the given error message into the logfile.
     *
     * @param msg The message to write into the logfile.
     * @param classobject The Object which calls the method.
     */
    public void error(String msg, Object classobject) {
        
        openLogFile();
        try {
            outputStream.write(new Date().toString()+"\t"+classname+"\t[ERROR] "+msg+"\n");
        } catch (IOException ex) {
            System.out.println("Couldn't write into the logfile!");
        }
        closeLogFile();
    }
    
}
